package com.feihao.zhou.controller;

import com.feihao.zhou.bean.Student;
import com.feihao.zhou.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/selectStudentServlet")
public class SelectStudentServlet extends HttpServlet {
    static UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前的请求：
        String name = req.getParameter("name");
        String age = req.getParameter("age");

        System.out.println(name);
        System.out.println(age);

        if ("".equals(name) && "".equals(age)) {
            //查询所有：
            List<Student> studentList = userService.selectAllStudent();
            req.getSession().setAttribute("studentList", studentList);
            req.getRequestDispatcher("/show.jsp").forward(req, resp);
        }

        if (name != "" && "".equals(age)) {
            //根据姓名查询学生：
            List<Student> studentList = userService.selectStudentByName(name);
            for (Student student : studentList) {
                System.out.println(student);
            }
            req.getSession().setAttribute("studentList", studentList);
            req.getRequestDispatcher("/show.jsp").forward(req, resp);
        }

        if ("".equals(name) && age != "") {
            //根据姓名查询学生：
            List<Student> studentList = userService.selectStudentByAge(age);
            req.getSession().setAttribute("studentList", studentList);
            req.getRequestDispatcher("/show.jsp").forward(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
